﻿
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace HDJ.Framework.Core
{
    public class TransitionGUIInspector<S, T> where S : StateDataBase, new() where T : StateTransitionDataBase, new()
    {


        public static void OnHeaderGUI(TransitionGUIData<T> line)
        {
            if (line == null)
                return;
            if (line.transitions.Count == 0)
                return;
            EditorGUIUtility.SetIconSize(Vector2.one * 32);
            GUIContent cc = EditorGUIUtility.IconContent("d_UnityEditor.VersionControl");
            cc.text = "";
            GUILayout.BeginHorizontal("LargeButton");
            GUILayout.Space(5);
            GUILayout.Label(cc);

            GUILayout.BeginVertical();

            GUILayout.Space(2);
            GUILayout.Label(line.transitions.Count + " Transitions");
            GUILayout.EndVertical();
            GUILayout.EndHorizontal();
            OnInspectorGUI(line);
        }

        static CustomTransitionGUIBase eInstance = null;
        private static void OnInspectorGUI(TransitionGUIData<T> line)
        {
            GUILayout.BeginVertical("box");
            for (int i = 0; i < line.transitions.Count; i++)
            {
                T t = line.transitions[i];
                S fromState = SM_EditorController<S, T>.Instance.machineData.GetState(t.fromStateID);
                S toState = SM_EditorController<S, T>.Instance.machineData.GetState(t.toStateID);
                GUILayout.BeginHorizontal("box");
                GUILayout.Label(fromState.name + " -> " + toState.name);
                if (line.transitions.Count > 1 && GUILayout.Button("-"))
                {
                    SM_EditorController<S, T>.Instance.machineData.DeleteTransition(t.transitionID);
                    return;
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndVertical();

            GUILayout.Space(5);
            if (eInstance == null)
                eInstance = (CustomTransitionGUIBase)EditorExtendAttributeUtils.GetEditorExtend(typeof(CustomTransitionGUIBase), typeof(T));
            if (eInstance == null)
            {
                eInstance = new CustomTransitionGUIBase();
            }
            eInstance.target = line;

            eInstance.OnInspectorGUI();
        }
    }
}
